package com.shteo.crmd.admin.service.impl;

import com.shteo.crmd.admin.dao.IGoodsDao;
import com.shteo.crmd.admin.service.IGoodsService;
import com.shteo.crmd.common.helper.SnowFlakeIDHelper;
import com.shteo.crmd.common.source.AKResult;
import com.shteo.crmd.model.goods.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Akuma on 2016/10/16.
 */
@Service
@Transactional
public class GoodsService implements IGoodsService {

    @Autowired
    private SnowFlakeIDHelper snowFlakeIDHelper;

    @Autowired
    private IGoodsDao goodsDao;

    @Override
    public AKResult addGoods(Goods goods) {
        goods.setId(snowFlakeIDHelper.nextId());
        goods.setCreateTime(new Date());
        goodsDao.addGoods(goods);
        return AKResult.builder().ok().errMsg("添加商品成功");
    }

    @Override
    public List<Goods> getAllGoods() {
        return goodsDao.getAllGoods();
    }

    @Override
    public Goods getGoodsById(Long goodsId) {
        return goodsDao.getGoodsById(goodsId);
    }

    @Override
    public AKResult delGoodsById(Long goodsId) {
        if (goodsDao.delGoodsById(goodsId)>0){
            return AKResult.builder().ok().errMsg("删除成功");
        }
        return AKResult.builder().fail().errMsg("删除失败");
    }

    @Override
    public AKResult updateGoodsById(Goods goods) {
        if (goodsDao.updateGoodsById(goods)>0){
            return AKResult.builder().ok().errMsg("更新成功");
        }
        return AKResult.builder().fail().errMsg("更新失败");
    }
}
